package com.xzr.hospitalregister.view;

import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;

import com.xzr.hospitalregister.R;
import com.xzr.hospitalregister.activity.IpSetActivity;
import com.xzr.hospitalregister.adapter.AdapterDialogIpSelect;
import com.xzr.hospitalregister.entity.DialogIpSelectItem;
import com.xzr.hospitalregister.util.DividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by Administrator on 2017/9/1.
 */

public class DialogIpSelect extends DialogFragment {
    private RecyclerView rv;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View inflate = inflater.inflate(R.layout.dialog_ip_select, null);
        initWidget(inflate);
        initData();
        return inflate;
    }

    private void initData() {
        SharedPreferences sp = getActivity().getSharedPreferences("IpSet", MODE_PRIVATE);
        final List<DialogIpSelectItem> data = new ArrayList<>();
        data.add(new DialogIpSelectItem("漯河市", sp.getString("ipValueCity1", "")));
        data.add(new DialogIpSelectItem("沁阳", sp.getString("ipValueCity2", "")));
        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        AdapterDialogIpSelect adapter = new AdapterDialogIpSelect(getActivity(), data);
        rv.setAdapter(adapter);
        rv.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST, R.drawable.divider_grey_bg));
        adapter.setOnItemClickListener(new AdapterDialogIpSelect.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                IpSetActivity activity = (IpSetActivity) getActivity();
                activity.setSelectIpToWidget(data.get(position));
                dismiss();
            }

            @Override
            public void onLongClick(int position) {

            }
        });
    }

    private void initWidget(View inflate) {
        rv = (RecyclerView) inflate.findViewById(R.id.rv_dialog_ip_select_list);
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, (int) (dm.heightPixels * 0.4));
        }
    }

}
